import { Component } from "react";
import PropTypes from "prop-types";

export default class Votemain extends Component{
    static propTypes={//校验参数类型
        supNum:PropTypes.number.isRequired,
        oppNum:PropTypes.number.isRequired
    }
    static defaultProps={//设置参数默认值
        supNum:0,
        oppNum:0
    }
    render(){
        let {supNum,oppNum}=this.props;
        return <div>
            <h2>支持人数：{supNum}</h2>
            <h2>反对人数：{oppNum}</h2>
            <h2>支持率：{this.computed()}</h2>
        </div>
    }
    computed=()=>{
      let {supNum,oppNum}=this.props;
      let total=supNum+oppNum;
      if(total<=0){
         return "0.00%";
      }
      return (supNum/total*100).toFixed(2)+"%";
    }
}